﻿
using System;
using System.IO;
namespace CSharp.Libs.Media
{
    public class PicLib : IDisposable
    {
        #region declarations
        private DataAccessLayer _picLibDatabase;
        #endregion



        #region constructor
        public PicLib()
        {
            _picLibDatabase = new DataAccessLayer();
        }

        #region dispose pattern (http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P)
        // some fields that require cleanup
        private bool disposed = false; // to detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // clean up managed handles
                    _picLibDatabase.Dispose();
                    _picLibDatabase = null;
                }

                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }
        #endregion
        #endregion



        #region private functions
        private PictureObject _getPictureObjectByFullFileName(
            string FullFileName)
        {
            if (File.Exists(FullFileName))
            {
                PictureObject po = new PictureObject();
                FileHashCalculator fhc = new FileHashCalculator();
                fhc.FullFileName = FullFileName;
                fhc.CalculateFileHash();

                po.FullFileName = FullFileName;
                po.Path = Path.GetDirectoryName(FullFileName);
                po.FileName = Path.GetFileName(FullFileName);
                po.Hash = fhc.CalculatedHash;

                return po;
            }
            else
            {
                //Datei existiert nicht. Melden? Dann null returnieren.

                return null;
            }
        }
        #endregion



        #region properties
        public DataAccessLayer Data { get { return _picLibDatabase; } }
        #endregion



        #region methods
        public PictureObject GetPictureObjectByFullFileName(
            string FullFileName)
        {
            return _getPictureObjectByFullFileName(FullFileName);
        }

        //public void GetFileByName() { }
        //public void GetFileByHash() { }


        
        
        
        //public void AddTagToFile(string FileName) { }

        //public PictureObject GetPictureObjectByFileName(string FileName) { return null; }
        //public PictureObject GetPictureObjectByHash(string Hash) { return null; }
        #endregion

    }
}
